home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / PERMISSIONMAPPING.PY < prev    next >
Encoding:
Python Source  |  2000-06-22  |  8.2 KB  |  223 lines

  1. ##############################################################################
  2. # Zope Public License (ZPL) Version 1.0
  3. # -------------------------------------
  4. # Copyright (c) Digital Creations.  All rights reserved.
  5. # This license has been certified as Open Source(tm).
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are
  8. # met:
  9. # 1. Redistributions in source code must retain the above copyright
  10. #    notice, this list of conditions, and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. #    notice, this list of conditions, and the following disclaimer in
  13. #    the documentation and/or other materials provided with the
  14. #    distribution.
  15. # 3. Digital Creations requests that attribution be given to Zope
  16. #    in any manner possible. Zope includes a "Powered by Zope"
  17. #    button that is installed by default. While it is not a license
  18. #    violation to remove this button, it is requested that the
  19. #    attribution remain. A significant investment has been put
  20. #    into Zope, and this effort will continue if the Zope community
  21. #    continues to grow. This is one way to assure that growth.
  22. # 4. All advertising materials and documentation mentioning
  23. #    features derived from or use of this software must display
  24. #    the following acknowledgement:
  25. #      "This product includes software developed by Digital Creations
  26. #      for use in the Z Object Publishing Environment
  27. #      (http://www.zope.org/)."
  28. #    In the event that the product being advertised includes an
  29. #    intact Zope distribution (with copyright and license included)
  30. #    then this clause is waived.
  31. # 5. Names associated with Zope or Digital Creations must not be used to
  32. #    endorse or promote products derived from this software without
  33. #    prior written permission from Digital Creations.
  34. # 6. Modified redistributions of any form whatsoever must retain
  35. #    the following acknowledgment:
  36. #      "This product includes software developed by Digital Creations
  37. #      for use in the Z Object Publishing Environment
  38. #      (http://www.zope.org/)."
  39. #    Intact (re-)distributions of any official Zope release do not
  40. #    require an external acknowledgement.
  41. # 7. Modifications are encouraged but must be packaged separately as
  42. #    patches to official Zope releases.  Distributions that do not
  43. #    clearly separate the patches from the original work must be clearly
  44. #    labeled as unofficial distributions.  Modifications which do not
  45. #    carry the name Zope may be packaged in any form, as long as they
  46. #    conform to all of the clauses above.
  47. # Disclaimer
  48. #   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
  49. #   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50. #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  51. #   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
  52. #   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  53. #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  54. #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  55. #   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  56. #   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  57. #   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  58. #   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59. #   SUCH DAMAGE.
  60. # This software consists of contributions made by Digital Creations and
  61. # many individuals on behalf of Digital Creations.  Specific
  62. # attributions are listed in the accompanying credits file.
  63. ##############################################################################
  64. """Permission Mapping
  65.  
  66. Sometimes, we need an object's permissions to be remapped to other permissions
  67. when the object is used in specual ways.  This is rather hard, since we
  68. need the object's ordinary permissions intact so we can manage it.
  69. """
  70.  
  71. import ExtensionClass, Acquisition
  72. from Permission import pname
  73. from Owned import UnownableOwner
  74.  
  75. class RoleManager:
  76.  
  77.         
  78.     def manage_getPermissionMapping(self):
  79.         """Return the permission mapping for the object
  80.  
  81.         This is a list of dictionaries with:
  82.  
  83.           permission_name -- The name of the native object permission
  84.  
  85.           class_permission -- The class permission the permission is
  86.              mapped to.
  87.         """
  88.         wrapper=getattr(self, '_permissionMapper', None)
  89.         if wrapper is None: wrapper=PM()
  90.  
  91.         perms={}
  92.         for p in self.possible_permissions():
  93.             perms[pname(p)]=p
  94.         
  95.         r=[]
  96.         a=r.append
  97.         for ac_perms in self.ac_inherited_permissions(1):
  98.             p=perms.get(getPermissionMapping(ac_perms[0], wrapper), '')
  99.             a({'permission_name': ac_perms[0], 'class_permission': p})
  100.         return r
  101.  
  102.     def manage_setPermissionMapping(self,
  103.                                     permission_names=[],
  104.                                     class_permissions=[], REQUEST=None):
  105.         """Change the permission mapping
  106.         """
  107.         wrapper=getattr(self, '_permissionMapper', None)
  108.         if wrapper is None: wrapper=PM()
  109.  
  110.         perms=self.possible_permissions()
  111.         for i in range(len(permission_names)):
  112.             name=permission_names[i]
  113.             p=class_permissions[i]
  114.             if p and (p not in perms):
  115.                 __traceback_info__=perms, p, i
  116.                 raise 'Permission mapping error', (
  117.                     """Attempted to map a permission to a permission, %s,
  118.                     that is not valid. This should never happen. (Waaa).
  119.                     """ % p)
  120.             
  121.  
  122.             setPermissionMapping(name, wrapper, p)
  123.  
  124.         self._permissionMapper=wrapper
  125.  
  126.         if REQUEST is not None:
  127.             return self.manage_access(
  128.                 self, REQUEST, 
  129.                 manage_tabs_message='The permission mapping has been updated')
  130.  
  131.     def _isBeingUsedAsAMethod(self, REQUEST =None, wannaBe=0):
  132.         try:
  133.             if hasattr(self, 'aq_self'):
  134.                 r=self.aq_acquire('_isBeingUsedAsAMethod_')
  135.             else:
  136.                 r=self._isBeingUsedAsAMethod_
  137.         except: r=0
  138.  
  139.         if REQUEST is not None:
  140.             if not r != (not wannaBe): REQUEST.response.notFoundError()
  141.  
  142.         return r
  143.  
  144.     def _isBeingAccessedAsZClassDefinedInstanceMethod(self):
  145.         p=getattr(self,'aq_parent',None)
  146.         if p is None: return 0          # Not wrapped
  147.         base=getattr(p, 'aq_base', None)
  148.         return type(base) is PermissionMapper  
  149.               
  150.                                         
  151.  
  152. def getPermissionMapping(name, obj, st=type('')):
  153.     obj=getattr(obj, 'aq_base', obj)
  154.     name=pname(name)
  155.     r=getattr(obj, name, '')
  156.     if type(r) is not st: r=''
  157.     return r
  158.  
  159. def setPermissionMapping(name, obj, v):
  160.     name=pname(name)
  161.     if v: setattr(obj, name, pname(v))
  162.     elif obj.__dict__.has_key(name): delattr(obj, name)
  163.  
  164. class PM(ExtensionClass.Base):
  165.     _owner=UnownableOwner
  166.  
  167.     _View_Permission='_View_Permission'
  168.         
  169.     def __getattr__(self, name):
  170.         # We want to make sure that any non-explicitly set methods are
  171.         # private!
  172.         if name[:1]=='_' and name[-11:]=="_Permission": return ''
  173.         raise AttributeError, name
  174.         
  175. PermissionMapper=PM
  176.  
  177. def aqwrap(object, wrapper, parent):
  178.     r=Rewrapper()
  179.     r._ugh=wrapper, object, parent
  180.     return r
  181.  
  182. class Rewrapper(ExtensionClass.Base):
  183.     def __of__(self, parent):
  184.         w, m, p = self._ugh
  185.         return m.__of__(
  186.             Acquisition.ImplicitAcquisitionWrapper(
  187.                 w, parent))
  188.  
  189.     def __getattr__(self, name):
  190.         w, m, parent = self._ugh
  191.         self=m.__of__(
  192.             Acquisition.ImplicitAcquisitionWrapper(
  193.                 w, parent))
  194.         return getattr(self, name)
  195.  
  196.     def __call__(self, *args, **kw):
  197.         w, m, parent = self._ugh
  198.         self=m.__of__(
  199.             Acquisition.ImplicitAcquisitionWrapper(
  200.                 w, parent))
  201.         return apply(self, args, kw)
  202.